﻿using IR.WPF.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IR.WPF.ViewModels.LandingViewModels;
using System.Windows;

namespace IR.WPF.UI.ViewModels
{

    public class VMVersionDetail : ViewModelBase
    {
        private string _versionID;
        private string _dateCreated;
        private string _status;
        private VMViewPage _parentContext;

        public VMVersionDetail(Window boundWindow)
        {
            BoundWindow = boundWindow;
            MainWindow window = App.Current.MainWindow as MainWindow;
            window.PropertyChanged += VersionPropertyChanged;
            _parentContext = BoundWindow.DataContext as VMViewPage;
            _parentContext.PropertyChanged += VersionPropertyChanged;
        }

        public override void DoCleanup()
        {
            MainWindow window = App.Current.MainWindow as MainWindow;
            window.PropertyChanged -= VersionPropertyChanged;
            _parentContext = BoundWindow.DataContext as VMViewPage;
            _parentContext.PropertyChanged -= VersionPropertyChanged;
            window.CurrentlySelectedVersion = null;
        }

        #region **Properties**
        public string VersionID
        {
            get { return (_versionID); }
            private set { _versionID = value; _parentContext.NotifyPropertyChanged("VersionID", this); }
        }

        public string DateCreated
        {
            get { return (_dateCreated); }
            private set { _dateCreated = value; _parentContext.NotifyPropertyChanged("DateCreated", this); }
        }

        public string Status
        {
            get { return (_status); }
            private set { _status = value; _parentContext.NotifyPropertyChanged("Status", this); }
        }
        #endregion

        #region **INotifyProperties**
        public void VersionPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CurrentlySelectedVersion")
            {
                MainWindow window = App.Current.MainWindow as MainWindow;
                VersionID = window.CurrentlySelectedVersion.VersionController.VersionID;
                DateCreated = window.CurrentlySelectedVersion.VersionController.DateCreated.ToString();
                Status = window.CurrentlySelectedVersion.VersionController.Status.ToString();
            }
            else if (e.PropertyName == "ViewPageClosed")
            {
                DoCleanup();
            }
        }
        #endregion
    }
}
